package com.xnyzc.lhy.mis.service.impl.cms.dict;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnyzc.lhy.common.component.config.GlobalConfig;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.dictionaries.OaSysDict;
import com.xnyzc.lhy.mis.entity.param.cms.dict.DictParam;
import com.xnyzc.lhy.mis.mapper.dictionaries.OaSysDictMapper;
import com.xnyzc.lhy.mis.service.cms.dict.DictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  字典管理
 * </p>
 *
 * @author wangshuo
 * @since 2019-08-24
 */
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private OaSysDictMapper oaSysDictMapper;

    /**
     * 获取字典管理列表
     *
     * @return
     */
    @Override
    public Rv getDictList(DictParam param) {
        Long page= param.getCurrent();
        QueryWrapper<OaSysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(OaSysDict.TYPE,"dict_type");
        if(StringUtils.isNotBlank(param.getType())){
            queryWrapper.eq(OaSysDict.TYPE,param.getType());
        }
        if(StringUtils.isNotBlank(param.getLabel())){
            queryWrapper.like(OaSysDict.LABEL,param.getLabel());
        }
        IPage<OaSysDict> iPage = new Page<>();
        iPage.setCurrent(page);
        iPage.setSize(20L);
        IPage<OaSysDict> dictList=oaSysDictMapper.selectPage(iPage, queryWrapper);
        return Rv.wrap(EErrorCode.success,dictList);
    }

    /**
     * 字典管理详情
     *
     * @return
     */
    @Override
    public Rv getDictDetails(DictParam param) {
        OaSysDict dict=oaSysDictMapper.selectById(param.getDictId());
        return Rv.wrap(EErrorCode.success,dict);
    }

    /**
     * 字典管理新增、修改
     *
     * @return
     */
    @Override
    public Rv getUpdateDict(DictParam param) {
        Long userId=TokenUtil.getTokenUserId();
        String tag=TokenUtil.getTag();
        Date date=new Date();
        QueryWrapper<OaSysDict> queryLabelWrapper = new QueryWrapper<>();
        queryLabelWrapper.eq(OaSysDict.LABEL,param.getType());
        queryLabelWrapper.eq(OaSysDict.TYPE,"dict_type");
        List<OaSysDict> dictLabelList=oaSysDictMapper.selectList(queryLabelWrapper);
        // 新增
        if(CheckUtil.objIsEmpty(param.getDictId())){
            QueryWrapper<OaSysDict> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(OaSysDict.TYPE,param.getType());
            queryWrapper.eq(OaSysDict.VALUE,param.getValue());
            List<OaSysDict> dictList=oaSysDictMapper.selectList(queryWrapper);
            if(!dictList.isEmpty()){
                throw PangException.create(EErrorCode.valueSame);
            }
            OaSysDict dict=new OaSysDict();
            dict.setDictId(IDUtil.nextId());
            dict.setValue(param.getValue());
            dict.setLabel(param.getLabel());
            dict.setType(param.getType());
            dict.setDescription(dictLabelList.get(0).getDescription());
            dict.setSort(param.getSort());
            dict.setCreateTime(date);
            dict.setUpdateTime(date);
            dict.setCreateUser(userId);
            dict.setTag(tag);
            dict.setUpdateUser(userId);
            // 侧边栏类型时存入图片
            if(StringUtils.isNotEmpty(param.getUrl())){
                dict.setExpand1(param.getUrl());
            }
            oaSysDictMapper.insert(dict);
        }
        // 修改
        else {
            QueryWrapper<OaSysDict> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(OaSysDict.TYPE,param.getType());
            queryWrapper.eq(OaSysDict.VALUE,param.getValue());
            queryWrapper.ne(OaSysDict.DICT_ID,param.getDictId());
            List<OaSysDict> dictList=oaSysDictMapper.selectList(queryWrapper);
            if(!dictList.isEmpty()){
                throw PangException.create(EErrorCode.valueSame);
            }
            OaSysDict dict=oaSysDictMapper.selectById(param.getDictId());
            dict.setValue(param.getValue());
            dict.setLabel(param.getLabel());
            dict.setType(param.getType());
            dict.setDescription(dictLabelList.get(0).getDescription());
            dict.setSort(param.getSort());
            dict.setUpdateTime(date);
            dict.setTag(tag);
            dict.setUpdateUser(userId);
            // 侧边栏类型时存入图片
            if(StringUtils.isNotEmpty(param.getUrl())){
                dict.setExpand1(param.getUrl());
            }
            oaSysDictMapper.updateById(dict);
        }
        return Rv.wrap(EErrorCode.updateSuccess);
    }

    /**
     * 删除字典配置
     *
     * @return
     */
    @Override
    public Rv getDeleteDict(DictParam param) {
        List<String> roleList=TokenUtil.getRoleDataScope();
        if(!roleList.contains(GlobalConfig.DATA_SCOPE_ALL)){
            throw PangException.create(EErrorCode.authErr);
        }
        if(CheckUtil.objIsEmpty(param.getDictId())){
            throw PangException.create(EErrorCode.missingArg);
        }
        oaSysDictMapper.deleteById(param.getDictId());
        return Rv.wrap(EErrorCode.deleteSuccess);
    }

    /**
     * 查询已有的字典类型
     *
     * @return
     */
    @Override
    public Rv getDictTypeList() {
        QueryWrapper<OaSysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSysDict.TYPE,"dict_type");
        List<OaSysDict> dictList=oaSysDictMapper.selectList(queryWrapper);
        return Rv.wrap(EErrorCode.success,dictList);
    }

    /**
     * 字典类型新增
     *
     * @return
     */
    @Override
    public Rv addDictType(DictParam param) {
        Long userId=TokenUtil.getTokenUserId();
        QueryWrapper<OaSysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSysDict.TYPE,"dict_type");
        queryWrapper.eq(OaSysDict.VALUE,param.getValue());
        List<OaSysDict> dictList=oaSysDictMapper.selectList(queryWrapper);
        if(!dictList.isEmpty()){
            throw PangException.create(EErrorCode.valueSame);
        }
        String tag=TokenUtil.getTag();
        Date date=new Date();
        // 新增
        OaSysDict dict=new OaSysDict();
        dict.setDictId(IDUtil.nextId());
        dict.setValue(param.getValue());
        dict.setLabel(param.getLabel());
        dict.setType("dict_type");
        dict.setDescription(param.getDescription());
        dict.setSort(param.getSort());
        dict.setCreateTime(date);
        dict.setUpdateTime(date);
        dict.setCreateUser(userId);
        dict.setTag(tag);
        dict.setUpdateUser(userId);
        oaSysDictMapper.insert(dict);
        return Rv.wrap(EErrorCode.updateSuccess);
    }

}
